package com.aiyige.page.my.customer.dialog;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.my.customer.adapter.DialogListAdapter;
import com.aiyige.utils.UIHelper;
import com.aiyige.utils.widget.BaseBottomDialogFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 客户类型选择弹出框
 */
public class CustomerTypeBottomDialog extends BaseBottomDialogFragment {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    @BindView(R.id.cancelBtn)
    TextView cancelBtn;

    Unbinder unbinder;

    DialogListAdapter adapter;

    List<String> list;

    String selectedString;

    public static CustomerTypeBottomDialog newInstance() {
        return new CustomerTypeBottomDialog();
    }

    Listener listener;
    int recyclerViewHeight;

    public void setLisenter(Listener callback) {
        this.listener = callback;
    }

    public void setData(List<String> list) {
        this.list = list;
    }


    public void setSelected(String selectedString) {
        this.selectedString = selectedString;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_customer_type, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        adapter = new DialogListAdapter();
        adapter.setSelected(selectedString);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.setNewData(list);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (list.get(position).equals(getActivity().getResources().getString(R.string.cancel))) {
                    dismiss();
                    return;
                }
                if (listener != null) {
                    listener.onSelect(list.get(position));
                }
                dismiss();
            }
        });
        if (recyclerViewHeight > 0) {
            ViewGroup.LayoutParams vl = recyclerView.getLayoutParams();
            vl.height = recyclerViewHeight;
            recyclerView.setLayoutParams(vl);
        }
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }


    public void setRecyclerViewHeight(int dp) {
        recyclerViewHeight = UIHelper.dip2px(dp);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public interface Listener {
        void onSelect(String value);
    }
}
